#!/bin/bash
# ----------------------------------------------------------------------
# neds handy rotating-filesystem-snapshot utility: monthly snapshots
# ----------------------------------------------------------------------
# intended to be run daily as a cron job 
# ----------------------------------------------------------------------

unset PATH

# ------------- system commands used by this script --------------------
ID=/usr/bin/id;
ECHO=/bin/echo;

MOUNT=/bin/mount;
RM=/bin/rm;
MV=/bin/mv;
CP=/bin/cp;

RSYNC=/usr/bin/rsync;

SSH=/usr/bin/ssh;
# ------------- file locations -----------------------------------------

SNAPSHOT_RW=/data/veni_backups; # backup directory on msl

BACKUP_DIR=/var/www/twiki; # directory to backup (local)

BACKUP=msl;  # backup server


# ------------- the script itself --------------------------------------

# make sure we're running as root
if (( `$ID -u` != 0 )); then { $ECHO "Sorry, must be root.  Exiting..."; exit; } fi

# attempt to chmod the directory as RW; else abort
$SSH $BACKUP chmod 600 $SNAPSHOT_RW ;
if (( $? )); then
{
	$ECHO "snapshot: could not chmod $SNAPSHOT_RW readwrite";
	exit;
}
fi;


# step 1: delete the oldest snapshot, if it exists:
$SSH $BACKUP "if [ -d $SNAPSHOT_RW/monthly.11 ] ; then			\
$RM -rf $SNAPSHOT_RW/weekly.11 ;				\
fi ;"

# step 2: move the old snapshost
$SSH $BACKUP "if [ -d $SNAPSHOT_RW/monthly.10 ] ; then			\
mv $SNAPSHOT_RW/monthly.10 $SNAPSHOT_RW/monthly.11 ;			\
fi ;"
$SSH $BACKUP "if [ -d $SNAPSHOT_RW/monthly.9 ] ; then			\
mv $SNAPSHOT_RW/monthly.9 $SNAPSHOT_RW/monthly.10 ;			\
fi ;"
$SSH $BACKUP "if [ -d $SNAPSHOT_RW/monthly.8 ] ; then			\
mv $SNAPSHOT_RW/monthly.8 $SNAPSHOT_RW/monthly.9 ;			\
fi ;"
$SSH $BACKUP "if [ -d $SNAPSHOT_RW/monthly.7 ] ; then			\
mv $SNAPSHOT_RW/monthly.7 $SNAPSHOT_RW/monthly.8 ;			\
fi ;"
$SSH $BACKUP "if [ -d $SNAPSHOT_RW/monthly.6 ] ; then			\
mv $SNAPSHOT_RW/monthly.6 $SNAPSHOT_RW/monthly.7 ;			\
fi ;"
$SSH $BACKUP "if [ -d $SNAPSHOT_RW/monthly.5 ] ; then			\
mv $SNAPSHOT_RW/monthly.5 $SNAPSHOT_RW/monthly.6 ;			\
fi ;"
$SSH $BACKUP "if [ -d $SNAPSHOT_RW/monthly.4 ] ; then			\
mv $SNAPSHOT_RW/monthly.4 $SNAPSHOT_RW/monthly.5 ;			\
fi ;"
$SSH $BACKUP "if [ -d $SNAPSHOT_RW/monthly.3 ] ; then			\
mv $SNAPSHOT_RW/monthly.3 $SNAPSHOT_RW/monthly.4 ;			\
fi ;"
$SSH $BACKUP "if [ -d $SNAPSHOT_RW/monthly.2 ] ; then			\
mv $SNAPSHOT_RW/monthly.2 $SNAPSHOT_RW/monthly.3 ;			\
fi ;"
$SSH $BACKUP "if [ -d $SNAPSHOT_RW/monthly.1 ] ; then			\
mv $SNAPSHOT_RW/monthly.1 $SNAPSHOT_RW/monthly.2 ;			\
fi ;"
$SSH $BACKUP "if [ -d $SNAPSHOT_RW/monthly.0 ] ; then			\
mv $SNAPSHOT_RW/monthly.0 $SNAPSHOT_RW/monthly.1 ;			\
fi ;"

# step 3: move the last weekly into the first monthly
$SSH $BACKUP "if [ -d $SNAPSHOT_RW/weekly.3 ] ; then			\
$CP -al $SNAPSHOT_RW/weekly.3 $SNAPSHOT_RW/monthly.0 ;				\
fi ;"

# now remount the directory as readonly

$SSH $BACKUP chmod 400 $SNAPSHOT_RW ;
if (( $? )); then
{
	$ECHO "snapshot: could not chmod $SNAPSHOT_RW readonly";
	exit;
} fi;
